package com.yhn.smartalbum.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 全局跨域配置
 *
 * @author yhn
 * @version 1.0
 * @description 跨域
 * @date 2024/1/13 16:17
 */
@Configuration
public class MyCorsConfiguration {
    @Bean
    public CorsWebFilter corsFilter() {
        // 配置CORS过滤器的源地址
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 设置CORS过滤器的配置信息
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 设置访问源地址
        corsConfiguration.addAllowedOriginPattern("*");
        // 设置源请求开头
        corsConfiguration.addAllowedHeader("*");
        // 设置访问源请求方法
        corsConfiguration.addAllowedMethod("*");
        // 设置允许携带cookie
        corsConfiguration.setAllowCredentials(true);
        // 对接口配置跨域设置
        source.registerCorsConfiguration("/**", corsConfiguration);
        // 返回对象处理跨域请求
        return new CorsWebFilter(source);
    }

}
